package cn.zktr.Personnel.controllers.tantiantian;

import java.io.IOException;
import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsZhiji;
import cn.zktr.Personnel.model.pojos.PerSlyPjt;
import cn.zktr.Personnel.model.pojos.PerSlySet;
import cn.zktr.Personnel.model.service.tantiantian.SlyPjtService;
import cn.zktr.Personnel.model.service.tantiantian.SlySetService;
import cn.zktr.Personnel.model.service.tantiantian.SlySlService;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.uitls.Homapape;


@Controller
public class SalaryAccountController {
	Map<String,Object> map;
	Map<String,Object> maps;
	/**
	 * 薪资套帐
	 */
	@Autowired
	private SlySetService slyser;
	
	/**
	 * 套帐项目
	 */
	@Autowired
	private SlyPjtService pjtser;
	/**
	 * 薪资发放
	 */
	@Autowired
	private SlySlService slser;

	/**
	 * 员工
	 */
	@Autowired
	private findService findser;
	
	//薪资套账
	@RequestMapping("ttt/salary-account")
	public void SalaryAccount(Model session){
		//查询所有薪资套账
		
		//查询所有套账项目
		List<PerSlyPjt> selectPgtAll = (List<PerSlyPjt>) pjtser.selectPgtAll();
		session.addAttribute("PerSlyPjt", selectPgtAll);		
	}
	//根据职务分组，查询所有职务
	@RequestMapping("addAccount_win_bumentext")
	@ResponseBody
	public JSONArray addAccount_win_bumentext(){
		return slyser.selePotZj();
	}
	//查询薪资员工上级，新资质部长
	@RequestMapping("addAccount_win_shenpitext")
	@ResponseBody
	public JSONArray addAccount_win_shenpitext(HttpSession session){
		return slyser.seleStmpotid(session);
	}
	@RequestMapping("Accounttable")
	@ResponseBody
	public Map<String,Object> SalaryAccounttable(int page,int rows,HttpSession session){
		Map<String,Object> map = new HashMap<>();
		map.put("total",slyser.selectSlyAlls().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray findAll=slyser.selectSlyAlls();
		//页内容
		map.put("rows",findAll);
		return map;
	}
	/**
	 * 薪资项目的添加
	 */
	@RequestMapping("add_accountXangmu")
	@ResponseBody
	public int add_accountXangmu(PerSlyPjt pjt,HttpSession session){
		PerSlyPjt addPjt = null;
		addPjt=slyser.AddPjt(pjt);
		if(addPjt==null) {
			return -1;
		}if(addPjt!=null) {
			return addPjt.getPjtid();
		}
		return 0;
	}

	/**
	 * @param pjt
	 * @param request
	 * @return获取选择项目的默认值
	 */
	@RequestMapping("Account_pjtidmrz")
	@ResponseBody
	public String Account_pjtidmrz(Integer pjtId,HttpServletRequest request){
		PerSlyPjt selePjt = slyser.selePjt(pjtId);
		return selePjt.getPjtidmrz();
	}

	/**
	 * @return
	 * 	//查询所有薪资项目addAccount_xiangmutable
	 */
	@RequestMapping("addAccount_xiangmutablesyc")
	@ResponseBody
	public Map<String,Object> addAccount_xiangmutabley(){
		return pjtser.selectSetAll();
	}
	/**
	 * @param p
	 * //根据职务查询员工
	 */
	@RequestMapping("Account_yuangongId")
	@ResponseBody
	public void  Account_yuangongId(PerPmsZhiji p){
		map=new HashMap<>();
		JSONArray pots=slyser.seleStmAll(p.getZhijiid());
		int i=0;
		for (Object object : pots) {
			JSONObject o=pots.getJSONObject(i);
			i++;
		}
		map.put("rows",pots);
		
		
	}
	/**
	 * @return返回员工股表数据
	 */
	@RequestMapping("Account_yuangong")
	@ResponseBody
	public Map<String, Object> Account_yuangong(){
		return map;	
	}
	
	/**
	 * @param set
	 * @param perPmsPot
	 * @param perPmsEmp2
	 * @param empids
	 * @param txid
	 * @param xiangmu
	 * @param session
	 * /套账相关字段的接收Account_yuangongtables,新增套帐
	 */
	@RequestMapping("Account_yuangongtables")
	@ResponseBody
	public void Account_yuangongtables(String 	setname,String setrem
			,int perPmsPot
			,int perPmsEmp2
			,String[] empids
			,String[] txid
			,String[] xiangmu,
			HttpSession session){
		//新增套账
		slyser.AddSlySet(setname,setrem, perPmsPot, perPmsEmp2 , session);
	}
	
	/**
	 * @param txid
	 * @param xiangmu
	 * 新增社保项目中间表
	 */
	@RequestMapping("Account_xiangmuzhong")
	@ResponseBody
	public void Account_xiangmuzhong(
			String[] txid
			,String[] xiangmu){

		//获取套账项目最新记录
		PerSlySet sets = slyser.selesetlimit();
		//新增套账项目中间表
		for (int s=0;s<txid.length-1;s++) {
			slyser.addtsx(Integer.parseInt(txid[s]),xiangmu[s],sets);
		}
	}
	
	/**
	 * @param empids
	 * 新增套帐员工
	 */
	@RequestMapping("Account_yuangongzhong")
	@ResponseBody
	public void Account_yuangongzhong(String[] empids){
		//新增套账
		// sets = slyser.selesetlimit();
		//新增套账人员
		for (int i=0;i<empids.length-1;i++) {
			slyser.addyxz(Integer.parseInt(empids[i]));
		}

	}
	
	/**
	 * @param setid
	 * @return Account_bianji薪资套账更改,返回选择那行的数据
	 */
	@RequestMapping("Account_bianji")
	@ResponseBody
	public String Account_bianji(Integer setid){
		String str="";
		JSONArray set = slyser.seleSetBySetId(setid);
		map=new HashMap<>();
		JSONArray Tx = slyser.seleBySetId_Tx(setid);
		map.put("rows", Tx);
		int i=0;
		for (Object object : set) {
			JSONObject json = set.getJSONObject(i);
			str+=json.getString("EMPNAME")+",";
					i++;
		}
		if(str.length()>0) {
			return str.substring(0,str.length()-1);
		}else if(str.equals("")) {
			return "无";
		}
		return null;
	}
	/**
	 * @return返回某套帐下的所有套帐项目
	 */
	@RequestMapping("addAccount_xiangmutables")
	@ResponseBody
	public Map<String, Object> addAccount_xiangmutables(){
		return map;	
	}
	/**
	 * @return修改薪资套帐
	 */
	@RequestMapping("update_set")
	@ResponseBody
	public void update_set(PerSlySet set){
		Optional<PerSlySet> PerSlySet = slser.seleSetId(set.getSetid());
		PerSlySet.get().setSetname(set.getSetname());
		PerSlySet.get().setSetrem(set.getSetrem());
		slyser.upSlySet(PerSlySet.get());
	}
	/**
	 * @return 
	 * @return带条件查询薪资套账
	 */
	@RequestMapping("sele_account")
	@ResponseBody
	public void sele_account(String str){
		maps = new HashMap<>();
		maps.put("total",slyser.seleBySetName("%"+str+"%").size());
		JSONArray findAll=slyser.seleBySetName("%"+str+"%");
		//页内容
		maps.put("rows",findAll);
	}
	@RequestMapping("sele_accounts")
	@ResponseBody
	public Map<String, Object> sele_accounts(){
		return maps;
	}
	/**
	 * @param session
	 * @return 返回创建人（登陆人）
	 */
	@RequestMapping("hopa_empname")
	@ResponseBody
	public String hopa_empname(HttpSession session){
		return findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")).getEmpname();
	}
	
	/**
	 * @param empid
	 * @return根据员工id查询套账
	 */
	@RequestMapping("sele_yuangongtaozhang")
	@ResponseBody
	public Map<String, Object> sele_yuangongtaozhang(Integer empid){
		return slyser.selectYuangongtaozhang(empid);
	}
	//送审Account_songshen
	@RequestMapping("Account_songshen")
	@ResponseBody
	public void Account_songshen(Integer setid,HttpSession session){
		slyser.songshen(setid,session);
	}
	//禁用Account_jinyong
	@RequestMapping("Account_jinyong")
	@ResponseBody
	public void Account_jinyong(Integer setid){
		slyser.jinyong(setid);
	}
	//启用Account_qiyong
	@RequestMapping("Account_qiyong")
	@ResponseBody
	public void Account_qiyong(Integer setid){
		slyser.qiyong(setid);
	}
	//根据职级找基本工资sele_jibengognzi
	@RequestMapping("sele_jibengognzi")
	@ResponseBody
	public String sele_jibengognzi(Integer zhijiid){
		return slyser.sele_jibengognzi(zhijiid).getContbs();
	}
}
